package com.adolph.netty.nio.communicate;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * 〈功能概述〉<br>
 *
 * @author: wangxiangjun
 * @date: 2020-10-02 14:53
 */

public class NIOClient {

    public static void main(String[] args) throws IOException {
        SocketChannel clientChannel = SocketChannel.open();
        //设置非阻塞
        clientChannel.configureBlocking(false);

        InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 8888);

        if(!clientChannel.connect(inetSocketAddress)){
            while (!clientChannel.finishConnect()){
                System.out.println("没有连接上！");
            }
        }

        System.out.println("连接成功！");
        String str = "客户端发起会话！";
        ByteBuffer wrap = ByteBuffer.wrap(str.getBytes());
        //写入数据
        clientChannel.write(wrap);
        System.in.read();
    }
}
